import Vue from "vue";
import VueRouter from "vue-router";
import Layout from "@/layout";
import store from "@/store";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    name: "Login",
    meta: {
      noAuth: true, // 不需要登录可访问
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "@/views/Login.vue"),
  },
  {
    path: "/",
    name: "Layout",
    component: Layout,
    children: [
      {
        path: "modelList",
        name: "modelList",
        component: () => import("@/views/modelList"),
      },
      {
        path: "model/edit",
        name: "modelEdit",
        component: () => import("@/views/modelList/edit.vue"),
      },

      {
        path: "mnist",
        name: "mnist",
        component: () => import("@/views/mnist"),
      },
      {
        path: "nonLinerSystem",
        name: "nonLinerSystem",
        component: () => import("@/views/nonLinerSystem"),
      },
      {
        path: "predictResultList",
        name: "predictResultList",
        component: () => import("@/views/predictResultList"),
      },
      {
        path: "about",
        name: "about",
        component: () => import("@/views/about"),
      },
    ],
  },
  {
    path: "/test",
    name: "Test",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ "@/views/Test.vue"),
  },
];

const router = new VueRouter({
  routes,
});

// 路由判断登录 根据路由配置文件的参数
router.beforeEach((to, from, next) => {
  // 判断该路由是否需要登录权限
  if (to.matched.some((record) => record.meta.noAuth)) {
    // 无需登录
    next();
  } else {
    // 需要登录
    // eslint-disable-next-line no-constant-condition
    if (store.getters["user/isLoggedIn"]) {
      // 已登陆
      next();
    } else {
      // 未登录跳转到登录页面
      next({
        path: "/login",
      });
    }
  }
});

export default router;
